<template>
  <div class="components-container">
    <!-- 新增 || 修改-->
    <sweet-modal ref="dialog1" class="dialog" :sweetModalMaxWidth="800">
      <h3>{{cdhtitle}}</h3>
      <div style=" height:300px; width:700px; margin:30px 50px">
        <div class="layui-form-item">
          <div class="layui-inline">
            <label class="layui-form-label">用车人</label>
             <div class="layui-input-inline">
              <el-input v-model="cdhdata.CDHDEADNAME" placeholder="用车人"></el-input>
            </div>
          </div>
          <div class="layui-inline">
            <label class="layui-form-label">车辆号码</label>
            <div class="layui-input-inline">
              <el-cascader :options="carLics" v-model="cdhextend.cardata">
              </el-cascader>
            </div>
          </div>
        </div>
        <div class="layui-form-item">
          <!-- 义乌的审批人 -->
          <div class="layui-inline" v-if="!showspr">
            <label class="layui-form-label">审批人</label>
             <div class="layui-input-inline">
              <el-select v-model="cdhdata.CDHLINKMAN">
                <el-option v-for="item in checkman" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </div>
          </div>
          <!-- 上虞的审批人 -->
          <div class="layui-inline" v-if="showspr"> 
            <label class="layui-form-label">审批人</label>
             <div class="layui-input-inline">
              <el-select v-model="cdhdata.CDHLINKMAN">
                <el-option v-for="item in shengpisy" :key="item.STAID" :label="item.STANAME" :value="item.STAID">
                </el-option>
              </el-select>
            </div>
          </div>
          <div class="layui-inline">
            <label class="layui-form-label">驾驶员</label>
            <div class="layui-input-inline">
              <el-select v-model="cdhdata.CDHDRIVER" placeholder="司机">
                <el-option v-for="item in drivers" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </div>
          </div>
        </div>
        <div class="layui-form-item">
          <div class="layui-inline">
            <label class="layui-form-label">出车时间</label>
            <div class="layui-input-inline chooseTime">
              <!-- CDHENDDATE  CDHENDTIME   CDHACTUALDATE  CDHACTUALTIME -->
              <!-- <el-date-picker v-date v-model="cdhActualDateTime" type="datetime" placeholder="选择时间"></el-date-picker> -->
              <el-date-picker style="width:115px;float:left;" clear-icon="null" type="date" v-date
                v-model="cdhActualDateTime"></el-date-picker>
              <el-time-picker style="width:75px;float:left" clear-icon="null"  v-model="cdhActualDateTime1"
                format="HH:mm"></el-time-picker>
            </div>
          </div>
          <div class="layui-inline">
            <label class="layui-form-label">返回时间</label>
            <div class="layui-input-inline chooseTime">
              <!-- <el-date-picker v-date v-model="cdhEndDateTime" type="datetime" placeholder="选择时间"> </el-date-picker> -->
              <el-date-picker style="width:115px;float:left;" clear-icon="null" type="date" v-date
                v-model="cdhEndDateTime"></el-date-picker>
              <el-time-picker style="width:75px;float:left" clear-icon="null"  v-model="cdhEndDateTime1"
                format="HH:mm"></el-time-picker>
            </div>
          </div>
        </div>
        <div class="layui-form-item">
          <div class="layui-inline">
            <label class="layui-form-label">出车里程<span class="text-danger">*</span></label>
            <div class="layui-input-inline">
              <el-input v-model="cdhdata.CDHMILEAGE" placeholder="里程"></el-input>
            </div>
          </div>
        </div>
         <div class="layui-form-item">
          <div class="layui-inline">
            <label class="layui-form-label">出车地址</label>
            <div class="layui-input-inline">
              <el-input v-model="cdhdata.CDHADDRESS" placeholder="出车地址" class="longtext"></el-input>
            </div>
          </div>
        </div>
        <div class="layui-form-item">
          <div class="layui-inline">
            <label class="layui-form-label">出车原因</label>
            <div class="layui-input-inline">
              <el-input type="textarea"  v-model="cdhdata.CDHREMARKS" class="longtext"></el-input>
            </div>
          </div>
        </div>
      </div>
      <div class="layui-form-item btnCenter">
        <el-button @click="cancel1">取消</el-button>
        <el-button type="primary" @click="submit" :loading="this.$store.state.isloading">提交</el-button>
      </div>
    </sweet-modal>
    <!--详情-->
    <sweet-modal ref="dialogxq" class="dialog" :sweetModalMaxWidth="800">
      <h3>{{cdhtitle}}</h3>
      <div style=" height:300px; width:700px; margin:30px 50px">
        <div class="layui-form-item">
           <div class="layui-inline">
            <label class="layui-form-label">用车人</label>
             <div class="layui-input-inline">
              <el-input v-model="cdhdata.CDHDEADNAME" disabled></el-input>
            </div>
          </div>
          <div class="layui-inline">
            <label class="layui-form-label">车辆号码</label>
            <div class="layui-input-inline">
              <el-cascader :options="carLics" v-model="cdhextend.cardata" disabled>
              </el-cascader>
            </div>
          </div>
        </div>
        <div class="layui-form-item">
          <div class="layui-inline" v-if="!showspr">
            <label class="layui-form-label">审批人</label>
             <div class="layui-input-inline">
              <el-select v-model="cdhdata.CDHLINKMAN" disabled>
                <el-option v-for="item in checkman" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </div>
          </div>
          <div class="layui-inline" v-if="showspr">
            <label class="layui-form-label">审批人</label>
             <div class="layui-input-inline">
              <el-select v-model="cdhdata.CDHLINKMAN" disabled>
                <el-option v-for="item in shengpisy" :key="item.STAID" :label="item.STANAME" :value="item.STAID">
                </el-option>
              </el-select>
            </div>
          </div>
          <div class="layui-inline">
            <label class="layui-form-label">驾驶员</label>
            <div class="layui-input-inline">
              <el-select v-model="cdhdata.CDHDRIVER" disabled>
                <el-option v-for="item in drivers" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </div>
          </div>
        </div>
        <div class="layui-form-item">
          <div class="layui-inline">
            <label class="layui-form-label">出车地址</label>
            <div class="layui-input-inline">
              <el-input v-model="cdhdata.CDHADDRESS" class="longtext" disabled></el-input>
            </div>
          </div>
        </div>
        <div class="layui-form-item">
          <div class="layui-inline">
            <label class="layui-form-label">出车原因</label>
            <div class="layui-input-inline">
              <el-input type="textarea"  class="longtext" v-model="cdhdata.CDHREMARKS" disabled>
              </el-input>
            </div>
          </div>
        </div>
        <div class="layui-form-item">
          <div class="layui-inline">
            <label class="layui-form-label">出车时间</label>
            <div class="layui-input-inline chooseTime">
              <!-- CDHENDDATE  CDHENDTIME   CDHACTUALDATE  CDHACTUALTIME -->
              <!-- <el-date-picker v-date v-model="cdhActualDateTime" type="datetime" placeholder="选择时间"></el-date-picker> -->
              <el-date-picker disabled style="width:115px;float:left;" clear-icon="null" type="date" v-date
                v-model="cdhActualDateTime"></el-date-picker>
              <el-time-picker disabled style="width:75px;float:left" clear-icon="null" v-date
                v-model="cdhActualDateTime1" format="HH:mm"></el-time-picker>
            </div>
          </div>
          <div class="layui-inline">
            <label class="layui-form-label">返回时间</label>
            <div class="layui-input-inline chooseTime">
              <!-- <el-date-picker v-date v-model="cdhEndDateTime" type="datetime" placeholder="选择时间"> </el-date-picker> -->
              <el-date-picker disabled style="width:115px;float:left;" clear-icon="null" type="date" v-date
                v-model="cdhEndDateTime"></el-date-picker>
              <el-time-picker disabled style="width:75px;float:left" clear-icon="null" v-date v-model="cdhEndDateTime1"
                format="HH:mm"></el-time-picker>
            </div>
          </div>

        </div>
        <div class="layui-form-item">
          <div class="layui-inline">
            <label class="layui-form-label">出车里程<span class="text-danger">*</span></label>
            <div class="layui-input-inline">
              <el-input v-model="cdhdata.CDHMILEAGE" placeholder="里程" disabled></el-input>
            </div>
          </div>
        </div>
      </div>
      <div class="layui-form-item btnCenter">
        <el-button @click="cancelxq">取消</el-button>
      </div>
    </sweet-modal>
    <!-- <printPdf ref="prints"></printPdf> -->
    <sweet-modal ref="dialogPrint" class="dialog" :sweetModalMaxWidth="pageWidth">
      <h3>{{cdhtitle}}</h3>
      <iframe :src="printUrl" style="width:1100px; height:500px;margin-top:20px;"></iframe>
    </sweet-modal>
  </div>
</template>

<style rel="stylesheet/scss" lang="scss" scoped>

</style>
<script>
  import sweetModal from "@/components/sweetModal";
  import apiBase from "@/api/apiBase.js";
  import {
    formatDate,
    timeStringToDate
  } from "@/utils/date.js";
  import {
    manage
  } from "@/mixins/managePage.js";
  import {
    PRINT
  } from "@/components/Pdf/PDF.js"
  const ADDS = "派车";
  const MODIFY = "修改";
  const DELETE = "删除";
  const INFO = "详情";
  const PRINTCARSEND = "打印派车单"
  export default {
    name: "carSendMain",
    mixins: [manage],
    data() {
      return {
        maxWidth: 1100,
        pageWidth: 1200,
        maxHeight: 500,
        printUrl: null,
        commonAddr: [],
        cdhdata: {
          CARCODNAME: null,
          CDHCARID: null,
          CDHLINKMAN: null,
          CDHDEADNAME:null, //用车人
          CDHDRIVER: null, //驾驶人
          CDHCARRY: null,
          CDHADDRESS: null,  //出车地址
          CDHADDID: null,
          CDHACTUALDATE: null,
          CDHACTUALTIME: null,
          CDHENDDATE: null,
          CDHENDTIME: null,
          CDHMILEAGE: null,
          CDHREMARKS: null,
          CDHOVERTIMEHOURS: null,
          CDHSTATUS: null,
        },
        cdhextend: {
          cardata: []
        },
        cdhActualDateTime: null,
        cdhActualDateTime1: null,
        cdhEndDateTime: null,
        cdhEndDateTime1: null,
        deaddata: {
          bussID: null,
          bussNO: null,
          deadName: null,
          linkMan: null,
          linkTel: null,
          deadRemark: null,
          pcrAddress: null,
          pcrDate: null,
          pcrCarType: null,
          cdhList: []
        },
        type: null,
        carLics: [],
        drivers: [],
        checkman: [],
        cdhTypes: [],
        title: null,
        cdhtitle: null,
        IsChargeAndSpecial: null,
        datetimes: null,
        adress: null,
        showspr: null, //审批人显隐个性化（上虞）
        shengpisy: null, //审批人显隐个性化（上虞）
      };
    },
    methods: {
      open(order, id, ...items) {
        this.order = order;
        this.cdhorder = order;

        this.datetimes = items[0];
        this.adress = items[1];
        switch (this.order) {
          case ADDS:
            this.cdhtitle = "派车新增";
            this.CdhdataRepeat();
            this.initSelect();
            this.$refs.dialog1.open();
            break;
          case MODIFY:
            if (id) {
              this.cdhdata.CDHID = id;
            } //余姚 后台在列表里反CDHID
            this.cdhtitle = "派车修改";
            this.initSelect();
            this.dataDetail();
            this.$refs.dialog1.open();
            break;
          case INFO:
            if (id) {
              this.cdhdata.CDHID = id;
            } //余姚 后台在列表里反CDHID
            this.cdhtitle = "派车详情";
            this.initSelect();
            this.dataDetail();
            this.$refs.dialogxq.open();
            break;
          case DELETE:
            if (id) {
              this.cdhdata.CDHID = id;
            } //余姚 后台在列表里反CDHID
            this.deleteCarInfo(id);
            break;
          default:
        }
      },
      operateCdh(order, cdhid) {
        this.cdhorder = order;
        if (cdhid) this.cdhdata.CDHID = cdhid;
        switch (this.cdhorder) {
          case MODIFY:
            this.cdhtitle = "派车修改";
            this.initSelect();
            this.dataDetail();
            this.$refs.dialog1.open();
            break;
          case PRINTCARSEND:
            this.cdhtitle = "派车单打印";
            //this.cdhorder, null, this.cdhdata.CDHID
            this.getPRINTSEND();
            this.$refs.dialogPrint.open();
            break;
          default:
        }
      },
      selectAddr() {
        this.cdhdata.CDHADDID = this.cdhdata.CDHADDRESS;
      },
      //提交
      submit() {

        switch (this.order) {
          case ADDS:
          case MODIFY:
            this.EditSendCar();
            break;
          case DELETE:
            break;
          default:
        }
      },
      //初始化下拉框
      initSelect() {
        this.getCar();
        this.getDrivers();
        this.getCheckman();
        this.getsprsyStatus();
      },

      //添加修改派车记录 
      EditSendCar() {
        var data = JSON.parse(JSON.stringify(this.cdhdata));
        data.CDHACTUALDATE = formatDate(this.cdhActualDateTime, "yyyyMMdd");
        data.CDHACTUALTIME = formatDate(this.cdhActualDateTime1, "hhmmss");
        data.CDHENDDATE = formatDate(this.cdhEndDateTime, 'yyyyMMdd');
        data.CDHENDTIME = formatDate(this.cdhEndDateTime1, 'hhmmss');
        data.BUSSID = this.deaddata.bussID;
        data.CDHCARID = this.cdhextend.cardata[this.cdhextend.cardata.length - 1];
        data.CensName = null;
        if (this.order == ADDS) {
          apiBase.DoService(
            data,
            apiBase.getApiAdress().Sendcar.CreateOtherSendCar,
            res => {
              this.$message.success("派车成功");
              this.$refs.dialog1.close();
              this.refreshData();
            },
            res => {
              this.$message.error(res.data.msg);
            }
          );
        } else if (this.order == MODIFY) {

          apiBase.DoService(
            data,
            apiBase.getApiAdress().Sendcar.Modify,
            res => {
              this.$message.success("修改成功");
              this.$refs.dialog1.close();
              this.refreshData();
            },
            res => {
              this.$message.error(res.data.msg);
            }
          );
        }
      },

      //详情 --派车
      dataDetail() {
        var data = {
          CDHID: this.cdhdata.CDHID
        };
        apiBase.DoService(data, apiBase.getApiAdress().Sendcar.DetailOtherSendCar, res => {
          this.cdhdata = res.data.content;
          this.cdhActualDateTime = timeStringToDate(this.cdhdata.CDHACTUALTIME, this.cdhdata.CDHACTUALDATE);
          this.cdhActualDateTime1 = timeStringToDate(this.cdhdata.CDHACTUALTIME, this.cdhdata.CDHACTUALDATE);
          this.cdhEndDateTime = timeStringToDate(this.cdhdata.CDHENDTIME, this.cdhdata.CDHENDDATE);
          this.cdhEndDateTime1 = timeStringToDate(this.cdhdata.CDHENDTIME, this.cdhdata.CDHENDDATE);
          this.cdhdata.CDHACTUALTIME = timeStringToDate(this.cdhdata.CDHACTUALTIME);
          this.cdhdata.CDHENDTIME = timeStringToDate(this.cdhdata.CDHENDTIME);
          this.cdhextend.cardata = [
            this.cdhdata.CARCODNAME,
            this.cdhdata.CDHCARID
          ];
          let findarry = this.carLics.filter(item => {
            return item.value === this.cdhdata.CARCODNAME;
          });
          if (findarry.length > 0) {
            let seachCarType = findarry[0];
            if (!seachCarType.children.some(item => {
                return item.value === this.cdhdata.CDHCARID;
              }))
              seachCarType.children.push({
                label: this.cdhdata.CARLIC,
                value: this.cdhdata.CDHCARID
              });
          }
          if (this.cdhdata.CDHDRIVER != "") {
            if (!this.drivers.some(item => {
                return item.value === this.cdhdata.CDHDRIVER;
              }))
              this.drivers.push({
                label: this.cdhdata.STANAME,
                value: this.cdhdata.CDHDRIVER
              });
          }
        });
      },
      //删除派车记录
      deleteCarInfo(chdid) {
        this.$confirm("此操作将永久删除该派车信息, 是否继续?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(callback => {
            var data = {
              CDHID: chdid
            };
            apiBase.DoService(data, apiBase.getApiAdress().Sendcar.Delete, res => {
              this.$message({
                message: "删除成功",
                type: "success"
              });
              this.refreshData();
            });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消"
            });
          });
      },
      //死亡人员数据重置
      DeaddataRepeat() {
        Object.assign(this.$data.deaddata, this.$options.data().deaddata);
      },
      //派车数据重置
      CdhdataRepeat() {
        Object.assign(this.$data.cdhdata, this.$options.data().cdhdata);
        Object.assign(this.$data.cdhextend, this.$options.data().cdhextend);
        this.cdhActualDateTime = new Date();
        this.cdhActualDateTime1 = new Date();
        this.cdhEndDateTime = null;
        this.cdhEndDateTime1 = null;
      },
      //获取车辆信息
      getCar() {
        let data = {};
        apiBase.DoService(
          data,
          apiBase.getApiAdress().Car.QueryNormalCarByType,
          res => {
            let cardata = res.data.content;
            let legendsCarData = cardata.map(item => ({
              label: item.CodeName,
              value: item.CodeValue,
              children: item.list_Cars.map(caritem => ({
                label: caritem.CARLIC,
                value: caritem.CARID
              }))
            }));
            this.carLics = legendsCarData;
          }
        );
      },
      //获取司机
      getDrivers() {
        let data = {};
        apiBase.DoService(data, apiBase.getApiAdress().Staff.QueryDriver, res => {
          let driverData = res.data.content,
            legendsDrivers = driverData.map(item => ({
              label: item.STANAME,
              value: item.STAID
            }));
          this.drivers = legendsDrivers;
        });
      },
      //获取司机
      getCheckman() {
        let data = {};
        apiBase.DoService(data, apiBase.getApiAdress().Staff.QueryNolstaff, res => {
          let driverData = res.data.content,
            legendsDrivers = driverData.map(item => ({
              label: item.STANAME,
              value: item.STAID
            }));
          this.checkman = legendsDrivers;
        });
      },
      //获取人员接口（上虞）
      getsprsyStatus() {
        let data = {};
        apiBase.DoService(
          data,
          apiBase.getApiAdress().Staff.QueryNolstaffYWY,   
          res => {
            this.shengpisy = res.data.content;
          }
        );
      },
      //获取业务状态
      getBussStatus() {
        let data = {};
        apiBase.DoService(
          data,
          apiBase.getApiAdress().Sendcar.GetBusstype,
          res => {
            this.bussStatus = res.data.content;
          }
        );
      },
      //获取接尸通知单URL
      getPRINTSEND() {
        let data = {
          PRPID: null,
          CDHID: this.cdhdata.CDHID
        };
        apiBase.DoService(
          data,
          apiBase.getApiAdress().Pdf.PrintCdh,
          res => {
            this.printUrl = res.data.content.PDFNAME;
            this.$refs.dialogPrint.open();
          });
      },
      //关闭派车信息弹窗
      cancel1() {
        this.$refs.dialog1.close();
      },
      //关闭派车信息弹窗 --详情
      cancelxq() {
        this.$refs.dialogxq.close();
      },
      //刷新
      refreshData() {
        this.$emit("refreshData");
      },
    },
    components: {
      sweetModal,
    },
    computed: {
    },
    created() {
      apiBase.DoService({},
        apiBase.getApiAdress().Param.GetIsChargeAndSpecial,
        res => {
          this.IsChargeAndSpecial = res.data.content
        });
      apiBase.DoService({},
        apiBase.getApiAdress().Param.GetIsShowNolstaffExcludeBYY,
        res => {
          this.showspr = res.data.content
        });
    }
  };

</script>
<style rel="stylesheet/scss" lang="scss" scoped>
  @import "src/styles/common.scss";

  .el-form-item {
    margin-bottom: 0px;
  }

  .div-card {
    max-height: 300px;
    overflow: auto;
  }

  .div-card label {
    min-width: 100px;
    font-weight: 400;
    padding-left: 20px;
  }

  .btnCenter {
    text-align: center;
  }

  .longtext {
    width: 492px;
  }

  .longertext {
    width: 770px;
  }

</style>
